//
//  UIColor+YCTools.m
//  Pods
//
//  Created by Apple on 2022/11/1.
//

#import "UIColor+YCTools.h"

@implementation UIColor (YCTools)

+ (UIColor *)colorWithHexString:(NSString *)hexString {
    return [self colorWithHexString:hexString alpha:1.0];
}

+ (UIColor *)colorWithHexString:(NSString *)hexString alpha:(float)opacity {
    
    NSString * cString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

    // String should be 6 or 8 characters
    if ([cString length] < 6) return [UIColor blackColor];

    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if (cString.length > 6)
    {
        cString = [cString substringFromIndex:cString.length - 6];
    }
    
    if ([cString length] != 6) return [UIColor blackColor];
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString * rString = [cString substringWithRange:range];

    range.location = 2;
    NSString * gString = [cString substringWithRange:range];

    range.location = 4;
    NSString * bString = [cString substringWithRange:range];

    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    return [UIColor colorWithRed:((float)r / 255.0f)
                           green:((float)g / 255.0f)
                            blue:((float)b / 255.0f)
                           alpha:opacity];
}



/// 渐变颜色
/// @param fromColor 开始颜色
/// @param toColor 结尾颜色
/// @param horizontal 是否水平方向
/// @param viewSize 赋值的视图size
+ (UIColor *)colorWithGradientFromColor:(UIColor *)fromColor
                                toColor:(UIColor *)toColor
                           isHorizontal:(BOOL)horizontal
                               viewSize:(CGSize)viewSize {
    return [UIColor colorWithGradientColors:@[fromColor, toColor] isHorizontal:horizontal viewSize:viewSize];
}

+ (UIColor *)colorWithGradientColors:(NSArray<UIColor *> *)colors isHorizontal:(BOOL)horizontal viewSize:(CGSize)viewSize {
    UIGraphicsBeginImageContextWithOptions(viewSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    
    NSMutableArray *bcolors = [NSMutableArray array];
    for (UIColor *color in colors) {
        [bcolors addObject:(__bridge id)color.CGColor];
    }
    
    CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (__bridge CFArrayRef)bcolors, NULL);
    CGPoint start;
    CGPoint end;
    if (horizontal)
    {
        start = CGPointMake(0.0, viewSize.height/colors.count);
        end = CGPointMake(viewSize.width, viewSize.height/colors.count);
    }else{
        start = CGPointMake(viewSize.width/colors.count, 0.0);
        end = CGPointMake(viewSize.width/colors.count, viewSize.height);
    }
    CGContextDrawLinearGradient(context, gradient, start, end, 0);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorspace);
    UIGraphicsEndImageContext();
    return [UIColor colorWithPatternImage:image];
}

@end
